package com.zmn.plat.admin.controller.brand.ratio;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.plat.business.interfaces.brand.ratio.BrandRatioBService;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.brand.BrandRatioModifyVo;
import com.zmn.plat.model.vo.brand.BrandRatioVo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 类描述:品牌系数管理修改控制
 *
 * @author linfeng
 * @since 2019/11/21/11:02
 */
@Controller
@RequestMapping("/brand/ratio/")
public class BrandRatioModifyController extends BrandRatioBaseController {
    private Logger logger = LoggerFactory.getLogger(BrandRatioModifyController.class);

    @Resource
    private BrandRatioBService brandRatioBService;

    /**
     * 删除品牌关联的系数
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    @RequiresPermissions("/brand/ratio/delete.action")
    public ResponseDTO delete(@RequestParam(value = "ids", defaultValue = "0") String ids,
                              @RequestParam(value = "priceType", defaultValue = "0") Integer priceType) {
        if (StringUtils.isEmpty(ids)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }

        brandRatioService.deleteByBrandIdsAndPriceType(ids.split(","), priceType);
        return ResponseDTO.success(ids, "删除成功！");
    }

    /**
     * 添加/修改品牌默认系数
     *
     * @param request
     * @param brandRatioVo
     * @return
     */
    @RequestMapping("addEdit")
    @ResponseBody
    @RequiresPermissions("/brand/ratio/brandRatio.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody BrandRatioVo brandRatioVo) {
        try {
            Integer brandId = brandRatioBService.addEditBrandRatio(getMcStaffRealName(), brandRatioVo);
            return ResponseDTO.success(brandId, "操作成功");
        } catch (PlatException ex) {
            return ResponseDTO.fail(ex.getMessage());
        }
    }

    /**
     * 添加或修改品牌分类系数
     *
     * @param request
     * @param brandRatioModifyVo
     * @return
     */
    @RequestMapping("categoryRatioAddEdit")
    @ResponseBody
    public ResponseDTO addEditCategoryRatio(HttpServletRequest request, @RequestBody BrandRatioModifyVo brandRatioModifyVo) {
        try {
            brandRatioBService.addEditCategoryRatio(getMcStaffRealName(), brandRatioModifyVo);
            return ResponseDTO.success(true, "操作成功");
        } catch (Exception e) {
            return ResponseDTO.fail("操作失败");
        }
    }
}
